﻿<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="()=>{this.visible=false}"
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="所属品牌" prop="BrandId">
          <a-select v-model="entity.BrandId" placeholder="所属品牌" style="width: 100%">
            <a-select-option v-for="item in BrandData" :key="item.Id" :value="item.Id">
              {{ item.Name }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="名称" prop="Name">
          <a-input v-model="entity.Name" autocomplete="off" placeholder="名称" />
        </a-form-model-item>
        <a-form-model-item label="排序" prop="Sort">
          <a-input v-model="entity.Sort" autocomplete="off" placeholder="越小越靠前" />
        </a-form-model-item>
        <a-form-model-item label="编码" prop="Code">
          <a-input v-model="entity.Code" autocomplete="off" placeholder="自定义编码" />
        </a-form-model-item>
        <a-form-model-item label="备注" prop="Remark">
          <a-textarea v-model="entity.Remark" autocomplete="off" placeholder="备注" :rows="5"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    parentObj: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      confirmLoading: false,
      loading: false,
      entity: {},
      BrandData: [],
      title: '',
      rules: {
        BrandId: [{ required: true, message: '请选择品牌名称', trigger: 'change' }],
        Name: [{ required: true, message: '请输入系列名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
      this.$http.post('/Elevator_Manage/Elevator_Brand/GetDataList', {}).then(resJson => {
        if (resJson.Success) {
          this.BrandData = resJson.Data
        }
      })
    },
    openForm (id, title) {
      this.init()
      this.title = title
      if (id) {
        this.loading = true
        this.$http.post('/Elevator_Manage/Elevator_Series/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
        })
      }
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/Elevator_Manage/Elevator_Series/SaveData', this.entity).then(resJson => {
          this.loading = false
          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.loading = false
            this.visible = false
            this.$emit('returnMain')
            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
            this.loading = false
          }
        })
      })
    }
  }
}
</script>
<style scoped>
</style>
